<?php
/*
 * @Author: 玛瑙
 * @Date:   2018-04-23 11:06:08
 * @Last Modified by:   Administrator
 * @Last Modified time: 2018-05-28 17:40:51
 */
namespace app\company\controller;

use think\Config;
use think\Request;

class Sqd extends Common
{
    /**
     * 申请单管理列表页面
     */
    public function index()
    {
        $map = [];
        $data = [
            'order_status' => '',
            'mobile'       => '',
            'license_type' => '',
            'start_time'   => '',
            'end_time'     => '',
            'user_name'    => '',
            'order_sn'     => '',
            'zt'           => '',
        ];
        //页码
        $pageNum = 1;
        if(Request::instance()->isPost()){
            $data = input('post.');
            if (!empty($data['mobile'])) {
                $map['o.mobile'] = ['like', "%{$data['mobile']}%"];
            }
            if ($data['order_status'] != '') {
                $map['order_status'] = $data['order_status'];
            } else {
                $map['order_status'] = ['in', '0,2,3,4'];
            }
            if ($data['zt'] != '') {
                if ($data['zt'] == 0 || $data['zt'] == 1) {
                    $map['o.zt'] = ['in', '0,1'];
                } else {
                    $map['o.zt'] = $data['zt'];
                }
            }
            if ($data['user_name'] != '') {
                $map['d.user_name'] = ['like', "%{$data['user_name']}%"];
            }
            if (!empty($data['license_type'])) {
                $map['o.license_type'] = $data['license_type'];
            }
            if (!empty($data['license_number'])) {
                $map['d.license_number'] = ['like', "%{$data['license_number']}%"];
            }
            if (!$data['order_sn'] == '') {
                if (strlen($data['order_sn']) == 23) {
                    $map['o.order_sn'] = $data['order_sn'];
                } else {
                    $map['o.order_sn'] = ['like', "%{$data['order_sn']}%"];
                }
            }
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $map['o.create_time'] = ['between', [strtotime($data['start_time']), strtotime($data['end_time']) + 86399]];
            } elseif (!empty($data['start_time']) && empty($data['end_time'])) {
                $map['o.create_time'] = ['between', [strtotime($data['start_time']), time()]];
            } elseif (empty($data['start_time']) && !empty($data['end_time'])) {
                $map['o.create_time'] = ['between', [0, strtotime($data['end_time']) + 86399]];
            }
            if (!empty($data['subscribe_date'])) {
                $map['o.subscribe_date'] = ['between', [strtotime($data['subscribe_date']), strtotime($data['subscribe_date']) + 86399]];
            }
            if (is_numeric(input('post.pageNum'))) {
                $pageNum = input('post.pageNum');
            }
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = 20;
        $data['numPerPage'] = $numPerPage;
        //列表数据
        $model = new \app\company\model\Sqd();
        $data['list'] = $model->get_list($map, $pageNum, $numPerPage, '');
        $data['count'] = $model->get_list($map, '', '', 1);
        $data['vehicle'] = db('vehicle')->select();
        return view('', $data);
    }

    /**
     * 详情
     */
    public function detail()
    {
        $data = [];
        $order_id = input('get.id');
        $order = db('order')->where('order_id', $order_id)->find();
        $order['nick_name'] = db('user_weixin')->where('id', '=', $order['user_weixin_id'])->value('nick_name');
        $map['user_weixin_id'] = $order['user_weixin_id'];
        $map['id'] = $order['driving_license_id'];
        $dl_info = get_info('driving_license', $map, '');
        if (empty($dl_info['image_id'])) {
            $dl_info['license_image'] = 'no-driving.png';
        } else {
            $dl_info['license_image'] = db('image')->where('id', '=', $dl_info['image_id'])->value('url');
        }
        $dl_info['license_image'] = Config::get('mp_image_url') . $dl_info['license_image'];
        $data['order_info'] = $order;
        $data['dl_info'] = $dl_info;
        return view('', $data);
    }

    /**
     * 订单打印
     */
    public function orderprint()
    {
        $map['order_id'] = input('id');
        $order_info = get_info('order', $map, '');
        $info = get_info('driving_license', $order_info['driving_license_id'], '');
        $company_name = get_value(session('company.company_id'), 'company', 'id', 'company_name', '0');
        $mobile = get_value(input('id'), 'order', 'order_id', 'mobile', 1);
        $data['company_id'] = session('company.company_id');
        $data['is_delete'] = 0;
        $list = db('inspection_item_model')->field('id,inspection_id,inspection_status')->where($data)->order('inspection_id')->select();
        $this->assign('list', $list);
        $company_info = db('company')->where('id', $data['company_id'])->field('company_name,company_addr')->find();
        $this->assign('company_info', $company_info);
        $this->assign('driving_license_info', $info);
        $this->assign('order_info', $order_info);
        $time = date("Y年m月d日");
        $this->assign('time', $time);
        $this->assign('company_name', $company_name);
        $this->assign('info', $info);
        $this->assign('mobile', $mobile);
        return $this->fetch('orderprint');
    }

    /**
     * 检验合格
     */
    public function modify()
    {
        $map['order_id'] = input('post.order_id');
        $map['next_time'] = strtotime(input('post.next_time'));
        $map['order_status'] = 3;
        $map['company_id'] = session('company.company_id');
        $map['finish_time'] = time();
        $map['is_get_gift'] = 1;
        try {
            doModify('order', $map);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message'    => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode'   => 200,
            'message'      => '操作成功',
            'navTabId'     => 'sqd_manage',
            'callbackType' => 'closeCurrent',
        ]);
    }

    /**
     * 发货页面
     */
    public function send()
    {
        $data['order_id'] = input('get.order_id');
        return view('', $data);
    }

    /**
     * 确认发货
     */
    public function doSend()
    {
        $map = input('post.');
        try {
            doModify('order', $map);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message'    => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode'   => 200,
            'message'      => '发货成功',
            'navTabId'     => 'sqd_manage',
            'callbackType' => 'closeCurrent',
        ]);
    }

    /**
     * 物流详情
     */
    public function wl()
    {
        $data['url'] = "https://m.kuaidi100.com/result.jsp?nu=" . input('get.nu');
        return view('', $data);
    }

    /**
     * 下次检车时间
     */
    public function next_time()
    {
        $data['order_id'] = input('get.order_id');
        return view('', $data);
    }
}
